package com.leetcode.no103;

import java.util.*;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        Deque<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new LinkedList<>();

        if (root == null) {
            return res;
        }

        // 记录每一层的
        List<Integer> tmpList = new LinkedList<>();

        TreeNode tmpNode;

        // 当前层的size
        int currentSize;
        int depth = 1;

        queue.offer(root);

        while (!queue.isEmpty()) {
            // 先获取当前层中有几个结点
            currentSize = queue.size();

            // 如果是偶数层
            if (depth % 2 == 0) {

                //正向遍历nodeList添加结果
                while (currentSize >= 0) {
                    // 从尾部拿
                    tmpNode = queue.pollLast();

                    tmpList.add(tmpNode.val);

                    if (tmpNode.left != null) {
                        queue.offer(tmpNode.left);
                    }
                    if (tmpNode.right != null) {
                        queue.offer(tmpNode.right);
                    }

                    currentSize--;
                }
            } else {
                // 反向遍历nodeList添加结果
                while (currentSize >= 0) {
                    // 从头部拿
                    tmpNode = queue.pollFirst();
                    tmpList.add(tmpNode.val);

                    if (tmpNode.left != null) {
                        queue.offer(tmpNode.left);
                    }
                    if (tmpNode.right != null) {
                        queue.offer(tmpNode.right);
                    }

                    currentSize--;
                }
            }

            // 到下一层了
            depth++;

            res.add(tmpList);
            // 清一下tmpList
            tmpList.clear();
        }

        return res;
    }


    public List<List<Integer>> zigzagLevelOrder01(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }

        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);

        boolean readLeft = true;

        // 一次性将当前deque中的元素都读取进来
        List<TreeNode> tmpNodeList = new ArrayList<>();

        // 每一层中存放的结果
        List<Integer> tmpNumList = new ArrayList<>();

        TreeNode tmpNode;

        while (!deque.isEmpty()) {
            // 一次将队列中的所有结点都读取进来
            while (!deque.isEmpty()) {
                tmpNodeList.add(deque.pollFirst());
            }

            if (readLeft) {
                // 正向遍历nodeList添加结果
                for (int i = 0; i < tmpNodeList.size(); i++) {
                    tmpNode = tmpNodeList.get(i);
                    tmpNumList.add(tmpNode.val);

                    // 为了保证队列是正确顺序的数据，需要对正向遍历从队列尾先添加左，再添加右
                    if (tmpNode.left != null) deque.offer(tmpNode.left);
                    if (tmpNode.right != null) deque.offer(tmpNode.right);

                }
                readLeft = false;
            } else {
                //反向遍历nodeList添加结果
                for (int i = tmpNodeList.size() - 1; i > 0; i--) {
                    tmpNode = tmpNodeList.get(i);
                    tmpNumList.add(tmpNode.val);

                    // 为了保证队列是正确顺序的数据，需要对反向遍历从队列尾先添加右，再添加左
                    if (tmpNode.right != null) deque.offer(tmpNode.right);
                    if (tmpNode.left != null) deque.offer(tmpNode.left);

                }
                readLeft = true;
            }

            res.add(tmpNumList);
            tmpNumList = new LinkedList<>();
            tmpNodeList = new LinkedList<>();
        }

        return res;
    }


    public List<List<Integer>> res;

    public List<List<Integer>> zigzagLevelOrder02(TreeNode root) {
        this.res = new ArrayList<>();

        if(root == null){
            return res;
        }

        travel(root, 0);
        return res;
    }

    private void travel(TreeNode cur, int level) {
        if (cur == null) {
            return;
        }

        //如果res.size() <= level说明下一层的集合还没创建，所以要先创建下一层的集合
        if (res.size() <= level) {
            List<Integer> newLevel = new LinkedList<>();
            res.add(newLevel);
        }

        // 遍历到第几层就操作哪一层的数据
        List<Integer> list = res.get(level);

        // 这里默认根节点是第0层
        // 偶数层相当于从左往右遍历 所以要添加到集合的末尾
        // 如果是奇数层相当于从右往左遍历，要把数据添加到集合的开头
        if (level % 2 == 0)
            list.add(cur.val);
        else
            list.add(0, cur.val);

        //分别遍历左右两个子节点，到下一层了，所以层数要加1
        travel(cur.left, level + 1);
        travel(cur.right, level + 1);

    }

    public static void main(String[] args) {
        Solution s1 = new Solution();
    }
}
